﻿var reEmpty = /^\s+$/;
var reEmail = /^.+\@.+\..+$/;
var reNumber = /^\d+$/;
var reFloat = /^(\d+)(,|.(\d+)|)$/;
var reTime = /^(\d+)(:(\d+)(am|pm|)|)$/i;


function trim(str) {
	var strRet = "";
	var nLength = str.length;
	var nFirst = 0;
	var nLast = nLength - 1;

	while ((nFirst < nLength) && (str.charAt(nFirst) <= ' ')) {
		nFirst++;
	}

	while ((nLast > nFirst) && (str.charAt(nLast) <= ' ')) {
		nLast--;
	}

	if (nFirst <= nLast) {
		strRet = str.substring(nFirst, nLast + 1);
	}

	return strRet;
}

function eraseChars(strSource, strErase, bWhitespace) {
	var strRet = "";
	var nIndex = 0;
	var nLength = strSource.length;
	var ch;

	while (nIndex < nLength) {
		ch = strSource.charAt(nIndex);

		if ((strErase.indexOf(ch) == -1) &&
		    !(bWhitespace && (ch <= ' '))
		  ) {
			strRet += ch;
		}

		nIndex++
	}

	return strRet;
}

function getInteger(str) {
	var nIndex = 0;
	var nLength = str.length;
	var nRet = -1;

	while ((nIndex < nLength) && (str.charAt(nIndex) == '0')) {
		nIndex++;
	}

	if (nIndex < nLength) {
		nRet = parseInt(str.substr(nIndex));

		if (isNaN(nRet)) {
			nRet = -1;
		}
	}
	else {
		nRet = 0;
	}

	return nRet;
}

function isEmpty(str) {
	var bRet = true;

	if (str != null) {
		if (str.length != 0) {
			bRet = reEmpty.test(str);
		}
	}

	return bRet;
}

function isNumber(str) {
	var bRet = false;

	if (!isEmpty(str)) {
		bRet = reNumber.test(trim(str));
	}

	return bRet;
}

function isFloat(str) {
	var bRet = false;

	if (!isEmpty(str)) {
		bRet = reFloat.test(trim(str));
	}

	return bRet;
}

function isEmail(str) {
	var bRet = true;

	if (!isEmpty(str)) {
		bRet = reEmail.test(trim(str));
	}

	return bRet;
}

function isTime(str) {
	var bRet = false;
	var nHour;
	var nMin;
	var strExt = "";
	var arMatch;
	var strTest;

	if (!isEmpty(str)) {
		strTest = eraseChars(str, "", true);
		arMatch = strTest.match(reTime);

		if (arMatch != null) {
			nHour = getInteger(arMatch[1]);
			if (arMatch[2].length > 0) {
				nMin = getInteger(arMatch[3]);

				if (arMatch[4].length > 0) {
					strExt = arMatch[4];
				}
			}
			else {
				nMin = 0;
			}

			if (strExt.length > 0) {
				bRet = (nHour >= 0) && (nHour <= 12) && (nMin >= 0) && (nMin < 60);
			}
			else {
				bRet = (nHour >= 0) && (nHour < 24) && (nMin >= 0) && (nMin < 60);
			}
		}
		else {
			bRet = false;
		}
	}

	return bRet;
}

function getFormatedTime(strTime, strDefaultExt) {
	var strFormatedTime = "";
	var nHour;
	var nMin;
	var strExt;
	var arMatch;
	var strTest;

	if (!isEmpty(strTime)) {
		strTest = eraseChars(strTime, "", true);
		arMatch = strTest.match(reTime);

		if (arMatch != null) {
			nHour = getInteger(arMatch[1]);
			if (arMatch[2].length > 0) {
				nMin = getInteger(arMatch[3]);

				if (arMatch[4].length > 0) {
					strExt = arMatch[4];
				}
				else {
					if ((nHour >= 0) && (nHour <= 12)) {
						strExt = strDefaultExt;
					}
					else {
						strExt = "";
					}
				}
			}
			else {
				nMin = 0;

				if ((nHour >= 0) && (nHour <= 12)) {
					strExt = strDefaultExt;
				}
				else {
					strExt = "";
				}
			}

			if ((nHour >= 0) &&
				((nHour <= 12) && (strExt != "") || (nHour <= 24) && (strExt == "")) &&
				(nMin >= 0) &&
				(nMin < 60)) {
				strFormatedTime = nHour.toString() + ":" + (nMin < 10 ? "0" : "") + nMin.toString() + strExt;
			}
			else {
				strFormatedTime = ""
			}
		}
	}

	return strFormatedTime;
}
